home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / Stlshape.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  51 lines

  1. //: C20:Stlshape.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Simple shapes w/ STL
  7. #include <vector>
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. class Shape {
  12. public:
  13.   virtual void draw() = 0;
  14.   virtual ~Shape() {};
  15. };
  16.  
  17. class Circle : public Shape {
  18. public:
  19.   void draw() { cout << "Circle::draw\n"; }
  20.   ~Circle() { cout << "~Circle\n"; }
  21. };
  22.  
  23. class Triangle : public Shape {
  24. public:
  25.   void draw() { cout << "Triangle::draw\n"; }
  26.   ~Triangle() { cout << "~Triangle\n"; }
  27. };
  28.  
  29. class Square : public Shape {
  30. public:
  31.   void draw() { cout << "Square::draw\n"; }
  32.   ~Square() { cout << "~Square\n"; }
  33. };
  34.  
  35. typedef std::vector<Shape*> Container;
  36. typedef Container::iterator Iter;
  37.  
  38. int main() {
  39.   Container shapes;
  40.   shapes.push_back(new Circle);
  41.   shapes.push_back(new Square);
  42.   shapes.push_back(new Triangle);
  43.   for(Iter i = shapes.begin();
  44.       i != shapes.end(); i++)
  45.     (*i)->draw();
  46.   // ... Sometime later:
  47.   for(Iter j = shapes.begin();
  48.       j != shapes.end(); j++)
  49.     delete *j;
  50. } ///:~
  51.